package com.level2app.ui.search;

import android.content.Intent;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.level2app.R;
import com.level2app.adapter.SearchFilterAdapter;
import com.level2app.base.BaseActivity;
import com.level2app.bean.SearchFilterChooseBean;
import com.level2app.databinding.ActivitySearchResultBinding;
import com.level2app.ui.search.view.SearchResultViewModel;
import com.level2app.utils.FragmentUtils;
import com.level2app.utils.JumpIntent;
import com.level2app.utils.KeyboardUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 搜索结果页
 */
public class SearchResultActivity extends BaseActivity<ActivitySearchResultBinding, SearchResultViewModel> {
    private final List<Fragment> mFragments = new ArrayList<>();
    private int currentItem = 0;
    private SearchFilterAdapter adapter;

    private String search;
    private List<String> filterData;

    @Override
    protected SearchResultViewModel bindModel() {
        return getViewModel(this, SearchResultViewModel.class);
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void initData() {
        initTitleBar(getString(R.string.search_result));
        binding.setMViewModel(viewModel);
        search = getIntent().getStringExtra("search");
//        mFragments.add(SearchResultFragment.newInstance(0));
        mFragments.add(SearchResultFragment.newInstance(1));
//        binding.vp.addOnPageChangeListener(mPageChangeListener);
//        MyFragmentPagerAdapter mAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), 1, mFragments);
//        binding.vp.setAdapter(mAdapter);
        FragmentUtils.add(getSupportFragmentManager(), mFragments, R.id.searchFl, 0);

        viewModel.searchData.setValue(search);
//        binding.mInputEt.setImeOptions(EditorInfo.IME_ACTION_SEND);

        binding.filterRv.setLayoutManager(new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false));
        adapter = new SearchFilterAdapter();
        binding.filterRv.setAdapter(adapter);

        filterData = new ArrayList<>();
        adapter.submitList(filterData);
        if (filterData.size() == 0) {
            binding.filterLl.setVisibility(View.GONE);
        } else {
            binding.filterLl.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void initClick() {
        super.initClick();
//        viewModel.onDelayClick(binding.offerTv, o -> {
//            if (currentItem != 0) {
//                currentItem = 0;
//                binding.vp.setCurrentItem(currentItem);
//            }
//        });
//        viewModel.onDelayClick(binding.campaignsTv, o -> {
//            if (currentItem != 1) {
//                currentItem = 1;
//                binding.vp.setCurrentItem(currentItem);
//            }
//        });
        viewModel.onDelayClick(binding.filterIv, o -> {
            JumpIntent.jump(this, SearchFilterActivity.class, 1000);
        });
        viewModel.onDelayClick(binding.deleteIv, o -> {
            viewModel.searchData.setValue("");
        });
//        viewModel.searchData.observe(this, s -> {
//            if (TextUtils.isEmpty(s)) {
//                binding.deleteIv.setVisibility(View.GONE);
//            } else {
//                binding.deleteIv.setVisibility(View.VISIBLE);
//            }
//        });
//        //回车搜索
//        binding.mInputEt.setOnEditorActionListener((v, actionId, event) -> {
//            if (actionId == EditorInfo.IME_ACTION_SEND || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
//                KeyboardUtils.hideSoftInput(this);
//                return true;
//            }
//            return false;
//        });
//        KeyboardUtils.registerSoftInputChangedListener(this, height -> {
//            if (height == 0) {
//                //软盘关闭
//                if (TextUtils.isEmpty(viewModel.searchData.getValue())) {
//
//                } else {
//                }
//                binding.mInputEt.clearFocus();
//            }
//        });
        viewModel.onDelayClick(binding.inputLl, o -> {
            setResult(RESULT_OK);
            finish();
        });
    }

//    private final ViewPager.OnPageChangeListener mPageChangeListener = new ViewPager.OnPageChangeListener() {
//        @Override
//        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//        }
//
//        @Override
//        public void onPageSelected(int position) {
//            currentItem = position;
//            if (position == 0) {
//                binding.campaignsTv.setBackground(null);
//                binding.campaignsTv.setTextColor(ContextCompat.getColor(SearchResultActivity.this, R.color.color_8d98af));
//                binding.campaignsTv.setTypeface(null, Typeface.NORMAL);
//                binding.offerTv.setBackgroundResource(R.drawable.shape_e9eded_32dp);
//                binding.offerTv.setTextColor(ContextCompat.getColor(SearchResultActivity.this, R.color.color_1e1e1e));
//                binding.offerTv.setTypeface(null, Typeface.BOLD);
//            } else {
//                binding.offerTv.setBackground(null);
//                binding.offerTv.setTextColor(ContextCompat.getColor(SearchResultActivity.this, R.color.color_8d98af));
//                binding.offerTv.setTypeface(null, Typeface.NORMAL);
//                binding.campaignsTv.setBackgroundResource(R.drawable.shape_e9eded_32dp);
//                binding.campaignsTv.setTextColor(ContextCompat.getColor(SearchResultActivity.this, R.color.color_1e1e1e));
//                binding.campaignsTv.setTypeface(null, Typeface.BOLD);
//            }
//        }
//
//        @Override
//        public void onPageScrollStateChanged(int state) {
//
//        }
//    };

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            //返回具有焦点的当前视图
            View v = getCurrentFocus();
            if (KeyboardUtils.isShouldHideInput(v, ev)) {
                KeyboardUtils.hideSoftInput(this);
            }
        }
        return super.dispatchTouchEvent(ev);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 1000) {
                SearchFilterChooseBean bean = (SearchFilterChooseBean) data.getSerializableExtra("date");
                if (bean != null) {
                    filterData.clear();
                    if (!TextUtils.isEmpty(bean.getSortString()))
                        filterData.add(bean.getSortString());
                    if (!TextUtils.isEmpty(bean.getDateString()))
                        filterData.add(bean.getDateString());
                    if (!TextUtils.isEmpty(bean.getPriceString()))
                        filterData.add(bean.getPriceString());
                }
                adapter.notifyDataSetChanged();
                if (filterData.size() == 0) {
                    binding.filterLl.setVisibility(View.GONE);
                } else {
                    binding.filterLl.setVisibility(View.VISIBLE);
                }
                //TODO:刷新列表
            }
        }
    }
}